class ListNode{
    public int val;//值
    public ListNode next;//后继信息
    public ListNode prev;//前驱信息

    public ListNode(int val) {
        this.val = val;
    }
}
public class DoubleLinkedList {
    public ListNode head;//头节点
    public ListNode last;//尾节点
    //头插法
    public void addFirst(int data){
        ListNode cur = new ListNode(data);
        if(this.head == null){
            this.head = cur;
            this.last = cur;
            return;
        }
        cur.next = this.head;
        head.prev = cur;
        head = cur;
    }
    //尾插法
    public void addLast(int data){
        ListNode cur = new ListNode(data);
        if(this.last == null){
            this.head = cur;
            this.last = cur;
            return;
        }
        this.last.next = cur;
        cur.prev = this.last;
        this.last = cur;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        if(index<0||index>size()) {
            throw new RuntimeException("index不合法！");
        }
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == size()){
            addLast(data);
            return;
        }
        ListNode node = new ListNode(data);
        ListNode ret = findIndex(index);
        node.next = ret;
        node.prev = ret.prev;
        ret.prev.next = node;
        ret.prev = node;

    }
    //查找下标为index的节点
    public ListNode findIndex(int index) {
        ListNode cur = this.head;
        while(index!=0){
            cur = cur.next;
            index--;
        }
        return cur;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = this.head;
        while(cur!=null){
            if(cur.val==key){
                return true;
            }
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        ListNode cur = this.head;
        while(cur!=null){
            if(cur.val == key){
                //删头节点
                if(cur == this.head){
                    this.head = head.next;
                    //判断是不是只有一个节点
                    if(this.head!=null){
                        this.head.prev = null;
                    }
                //删其他节点(中间节点、尾节点)
                }else{
                    cur.prev.next = cur.next;
                    //判断是不是尾节点
                    if(cur.next!=null){
                        cur.next.prev = cur.prev;
                    }else{
                        this.last = last.prev;
                    }
                }
                return;
            }else{
                cur = cur.next;
            }
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        ListNode cur = this.head;
        while(cur!=null){
            if(cur.val == key){
                //删头节点
                if(cur == this.head){
                    this.head = head.next;
                    //判断是不是只有一个节点
                    if(this.head!=null){
                        this.head.prev = null;
                    }
                    //删其他节点(中间节点、尾节点)
                }else{
                    cur.prev.next = cur.next;
                    //判断是不是尾节点
                    if(cur.next!=null){
                        cur.next.prev = cur.prev;
                    }else{
                        this.last = last.prev;
                    }
                }
            }
            cur = cur.next;
        }
    }
    //得到单链表的长度
    public int size(){
        int count = 0;
        ListNode cur = this.head;
        while(cur!=null){
            count++;
        }
        return count;
    }
    public void myToString(){
        ListNode cur = this.head;
        while(cur!=null){
            System.out.print(cur.val+" ");
        }
        System.out.println();
    }
    public void clear(){
        ListNode cur = this.head;
        while(cur!=null){
            ListNode curNext = cur.next;
            //如果是引用类型的数据则cur.val = null
            cur.prev = null;
            cur.next = null;
            cur = curNext;
        }
        this.head = null;
        this.last = null;
    }
}
